package com.vksyu.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.springframework.stereotype.Component;

import com.vksyu.model.CommonData;
import com.vksyu.model.CommonResults;

@Component
public class RatingBilder {
	
	private final static Logger log = Logger.getLogger(RatingBilder.class.getName());

	private final static double[] coeficient = { 0.196, // personalIndex;
			0.264, // edicationIndex;
			0.149, // internationIndex;
			0.113, // publishingIndex
			0.142, // indecationIndex;
			0.136 };// contentIndex;

	public List<CommonData> getUniversityRating(List<CommonData> universityData) {
		List<CommonData> universityRating = new ArrayList<CommonData>();
		for (CommonData university : universityData) {
			double param1 = university.getParam1();
			double param2 = university.getParam2();
			double param3 = university.getParam3();
			double vksyu = (param1 * coeficient[0] + param2 * coeficient[1] + param3
					* coeficient[2]) / 3;
			universityRating.add(new CommonData(university.getId(), university
					.getName(), university.getParam1(), university.getParam2(),
					university.getParam3(), vksyu));
		}
		return universityRating;
	}
	
	public List<CommonData> getJornalsRating(List<CommonData> jornalsData) {
		List<CommonData> jornalsRating = new ArrayList<CommonData>();
		for (CommonData jornal : jornalsData) {
			double param1 = jornal.getParam1();
			double param2 = jornal.getParam2();
			double param3 = jornal.getParam3();
			double vksyu = (param1 * coeficient[3] + param2 * coeficient[4] + param3
					* coeficient[5]) / 3;
			jornalsRating.add(new CommonData(jornal.getId(), jornal
					.getName(), jornal.getParam1(), jornal.getParam2(),
					jornal.getParam3(), vksyu));
		}
		return jornalsRating;
	}
	
	private List<CommonData> getAverageJornalsData(List<CommonData> universitiesData,
											   	  List<CommonData> jornalsData){
		List<CommonData> averageJornalsData = new ArrayList<CommonData>();
		log.info("Input data param 1:"+ jornalsData.get(0).getParam1());
		
		for (CommonData university: universitiesData){
			int jornalCount = 0;
			boolean isUniversityHasJornal = false;
			double param1 = 0;
			double param2 = 0;
			double param3 = 0;
			for(CommonData jornal: jornalsData){
				if (university.getId() == jornal.getId()){
					param1 += jornal.getParam1();
					param2 += jornal.getParam2();
					param3 += jornal.getParam3();
					jornalCount++;
					isUniversityHasJornal = true;
				}
			}
			if (isUniversityHasJornal){
				param1 /= jornalCount;
				param2 /= jornalCount;
				param3 /= jornalCount;
			} else {
				param1 = param2 = param3 = 0;
			}
			
			averageJornalsData.add(new CommonData(university.getId(), university.getName(),
												  param1, param2, param3));
			jornalCount = 0;
		}
		log.info("Output data param 1:"+ averageJornalsData.get(0).getParam1());
		return averageJornalsData;
	}
	
	public List<CommonResults> getCommonResults(List<CommonData> universitiesData,
			   									List<CommonData> jornalsData){
		List<CommonResults> commonResults = new ArrayList<CommonResults>();
		List<CommonData> averageJornalsData = getAverageJornalsData(universitiesData, jornalsData);
		log.info("Input data param 1 inRusults:"+ averageJornalsData.get(0).getParam1());
		for (int i = 0; i < universitiesData.size(); i++){
			double[] params = new double[coeficient.length]; 
			
			params[0] = universitiesData.get(i).getParam1();
			params[1] = universitiesData.get(i).getParam2();
			params[2] = universitiesData.get(i).getParam3();
			
			if (i <= averageJornalsData.size()){
				params[3] = averageJornalsData.get(i).getParam1();
				params[4] = averageJornalsData.get(i).getParam2();
				params[5] = averageJornalsData.get(i).getParam3();
			} else {
				params[3] = 0;
				params[4] = 0;
				params[5] = 0;
			}
			
			double vksyu = 0;
			for (int j = 0; j < coeficient.length; j++){
				vksyu += params[j] * coeficient[j];
			}
			vksyu /=6;
			commonResults.add(new CommonResults(universitiesData.get(i).getId(),
												universitiesData.get(i).getName(),
												params[0],
												params[1],
												params[2],
												params[3],
												params[4],
												params[5],
												vksyu));
		}
		log.info("Output data vksyu inRusults:"+ commonResults.get(0).getVksyu());
		return commonResults;
	}
	
	public static List<Double> getOnlyVKSYUFromResults(List<CommonResults> results){
		List<Double> vksyu = new ArrayList<Double>();
		
		for (CommonResults result: results){
			vksyu.add(result.getVksyu());
		}
		
		return vksyu;
	}
	
	public static List<Double> getOnlyVKSYU(List<CommonData> results){
		List<Double> vksyu = new ArrayList<Double>();
		
		for (CommonData result: results){
			vksyu.add(result.getVksyu());
		}
		
		return vksyu;
	}
}
